import { HttpClient, HttpParams } from '@angular/common/http';
import { Inject, Injectable, Injector } from '@angular/core';
import { Observable } from 'rxjs';
import { API_CONFIG, ServicesModule } from './services.module';
import { map, pluck, switchMap } from 'rxjs/internal/operators';
import { SheetList, Singer, Song, SongSheet } from './data-types/common.types';
import { SongService } from './song.service';
import * as queryString from 'query-string';

export type SheetParams = {
  offset: number;
  limit: number;
  order: 'new' | 'hot';
  cat: string;
}
@Injectable({
  providedIn: ServicesModule
})
export class SheetService {

  constructor(
    private http:HttpClient,
    @Inject(API_CONFIG) private uri:string,
    private songService: SongService
  ) { }

  // 获取歌单列表
  getSheets(args: SheetParams):  Observable<SheetList>{
    const params = new HttpParams({ fromString: queryString.stringify(args)})
    return this.http.get(this.uri + 'top/playlist',{params})
    .pipe(map((res:any)=> res ));
  }

  // 获取歌单详情
  getSongSheetDetail(id:number): Observable<SongSheet>{
    const params = new HttpParams().set("id",id.toString())
    return this.http.get(this.uri + 'playlist/detail',{params})
    .pipe(map((res:any)=>res.playlist));
  }

  playSheet(id:number):Observable<Song[]>{
    return this.getSongSheetDetail(id)
    .pipe(pluck('tracks'),switchMap((tracks:any)=> this.songService.getSongList(tracks)))
  }
}
